home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / telecomm / sticpsrc.lzh / SOURCE.ARC / TRACE.C < prev    next >
C/C++ Source or Header  |  1989-08-09  |  4KB  |  185 lines

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include "global.h"
  4. #include "mbuf.h"
  5. #include "iface.h"
  6. #include "trace.h"
  7.  
  8. FILE *trfp = stdout;            /* file pointer used for tracing */
  9. int trcount = 0;            /* used to close file for flushing */
  10. char trname[40];            /* name if not stdout */
  11.  
  12. /* Redefined here so that programs calling dump in the library won't pull
  13.  * in the rest of the package
  14.  */
  15. static char nospace[] = "No space!!\n";
  16.  
  17. dump(interface,direction,type,bp)
  18. register struct interface *interface;
  19. int direction;
  20. unsigned type;
  21. struct mbuf *bp;
  22. {
  23.     struct mbuf *tbp;
  24.     void ascii_dump(),hex_dump();
  25.     int ax25_dump(),ether_dump(),ip_dump();
  26.     int (*func)();
  27.     int16 size;
  28.     long trtime;
  29.  
  30.     if((interface->trace & direction) == 0)
  31.         return; /* Nothing to trace */
  32.  
  33.     time(&trtime);            /* get time to show it in trace */
  34.     fprintf(trfp,"%.15s %s ",ctime(&trtime) + 4,interface->name);
  35.  
  36.     switch(direction){
  37.     case IF_TRACE_IN:
  38.         fprintf(trfp,"recv:\n");
  39.         break;
  40.     case IF_TRACE_OUT:
  41.         fprintf(trfp,"sent:\n");
  42.         break;
  43.     }
  44.     if(bp == NULLBUF || (size = len_mbuf(bp)) == 0){
  45.         fprintf(trfp,"empty packet!!\n");
  46.         goto trdone;
  47.     }
  48.  
  49.     if(type < NTRACE)
  50.         func = tracef[type];
  51.     else
  52.         func = NULLFP;
  53.  
  54.     dup_p(&tbp,bp,0,size);
  55.     if(tbp == NULLBUF){
  56.         fprintf(trfp,nospace);
  57.         if (trfp != stdout)
  58.             printf(nospace);
  59.         goto trdone;
  60.     }
  61.     if(func != NULLFP)
  62.         (*func)(&tbp,1);
  63.     if(interface->trace & IF_TRACE_ASCII){
  64.         /* Dump only data portion of packet in ascii */
  65.         ascii_dump(&tbp);
  66.     } else if(interface->trace & IF_TRACE_HEX){
  67.         /* Dump entire packet in hex/ascii */
  68.         free_p(tbp);
  69.         dup_p(&tbp,bp,0,len_mbuf(bp));
  70.         if(tbp != NULLBUF)
  71.             hex_dump(&tbp);
  72.         else {
  73.             fprintf(trfp,nospace);
  74.             if (trfp != stdout)
  75.                 printf(nospace);
  76.         }
  77.     }
  78.     free_p(tbp);
  79. trdone:
  80.     if(trfp != stdout){
  81. #ifndef ATARI_ST
  82.         fflush(trfp);
  83. #endif
  84. #if (defined(MSDOS) || defined(ATARI_ST))
  85.         if(++trcount > 25){
  86.             fclose(trfp);
  87.             trfp = fopen(trname,"a");
  88.             trcount = 0;
  89.         }
  90. #endif
  91.         if(trfp == NULLFILE || ferror(trfp)){
  92.             printf("Error on %s - trace to console\n",trname);
  93.             if(trfp != NULLFILE && trfp != stdout)
  94.                 fclose(trfp);
  95.             trfp = stdout;
  96.         }
  97.     }
  98.     fflush(stdout);
  99. }
  100.  
  101. /* Dump an mbuf in hex */
  102. void
  103. hex_dump(bpp)
  104. register struct mbuf **bpp;
  105. {
  106.     int16 n;
  107.     int16 address;
  108.     void fmtline();
  109.     char buf[16];
  110.  
  111.     if(bpp == NULLBUFP || *bpp == NULLBUF)
  112.         return;
  113.  
  114.     address = 0;
  115.     while((n = pullup(bpp,buf,sizeof(buf))) != 0){
  116.         fmtline(address,buf,n);
  117.         address += n;
  118.     }
  119. }
  120. /* Dump an mbuf in ascii */
  121. void
  122. ascii_dump(bpp)
  123. register struct mbuf **bpp;
  124. {
  125.     char c;
  126.     register int16 tot;
  127.  
  128.     if(bpp == NULLBUFP || *bpp == NULLBUF)
  129.         return;
  130.  
  131.     tot = 0;
  132.     while(pullup(bpp,&c,1) == 1){
  133.         if((tot % 64) == 0)
  134.             fprintf(trfp,"%04x  ",tot);
  135.         putc(((!isascii(c) || isprint(c)) ? c : '.'),trfp);
  136.         if((++tot % 64) == 0)
  137.             fprintf(trfp,"\n");
  138.     }
  139.     if((tot % 64) != 0)
  140.         fprintf(trfp,"\n");
  141. }
  142. /* Print a buffer up to 16 bytes long in formatted hex with ascii
  143.  * translation, e.g.,
  144.  * 0000: 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f  0123456789:;<=>?
  145.  */
  146. void
  147. fmtline(addr,buf,len)
  148. int16 addr;
  149. char *buf;
  150. int16 len;
  151. {
  152.     char line[80];
  153.     register char *aptr,*cptr;
  154.     register char c;
  155.     void ctohex();
  156.  
  157.     memset(line,' ',sizeof(line));
  158.     ctohex(line,hibyte(addr));
  159.     ctohex(line+2,lobyte(addr));
  160.     aptr = &line[6];
  161.     cptr = &line[55];
  162.     while(len-- != 0){
  163.         c = *buf++;
  164.         ctohex(aptr,(int16)uchar(c));
  165.         aptr += 3;
  166.         c &= 0x7f;
  167.         *cptr++ = (!isascii(c) || isprint(c)) ? c : '.';
  168.     }
  169.     *cptr = '\0';
  170.     fprintf(trfp,"%s\n",line);
  171. }
  172. /* Convert byte to two ascii-hex characters */
  173. static
  174. void
  175. ctohex(buf,c)
  176. register char *buf;
  177. register int16 c;
  178. {
  179.     static char hex[] = "0123456789abcdef";
  180.  
  181.     *buf++ = hex[hinibble(c)];
  182.     *buf = hex[lonibble(c)];
  183. }
  184.  
  185.